package itbour.onetouchshow.activity.version1_6;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.video.make.MakeActivity;
import itbour.onetouchshow.activity.video.make.MakePresenter;
import itbour.onetouchshow.listener.SoftKeyBoardListener;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.UIUtils;

/**
 * Created by Administrator on 2018/5/16.
 */

public class MakeActivityVTest extends MakeActivity {

    @BindView(R.id.it_hint_view)
    protected LinearLayout hintView;
    VideoEditAdapterVTest videoEditAdapter;
    LinearLayoutManager linearLayoutManager;
    LinearLayout.LayoutParams layoutParams;
    boolean move = false;
    private int currentKeyboardH;
    private int screenHeight;
    private int editTextBodyHeight;

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        mPresenter = new MakePresenter(this, this);
        super.afterCreate(savedInstanceState);
        llRoot.setVisibility(View.GONE);
    }

    @Override
    protected void initKeyBroadListener() {
        SoftKeyBoardListener.setListener(MakeActivityVTest.this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                L_.i("键盘显示 高度" + height);
                if (llBootmView != null) {
                    llBootmView.setVisibility(View.GONE);
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
                    layoutParams.weight = height;
                    hintView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void keyBoardHide(int height) {
                // linearLayout.setVisibility(View.GONE);
                // idRvData.scrollToPosition(videoEditAdapter.getMpostion());
                L_.i("键盘隐藏 高度" + height);
                // videoEditAdapter.getEditTextView().clearFocus();
                if (llBootmView != null) {
                    llBootmView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (llBootmView != null) {
                                llBootmView.setVisibility(View.VISIBLE);
                                hintView.setVisibility(View.GONE);
                            }
                        }
                    }, 30);
                }
            }
        });
    }

    protected void initRvEditVideo() {
        linearLayoutManager = new LinearLayoutManager(MakeActivityVTest.this, LinearLayoutManager.VERTICAL, false);
        videoEditAdapter = new VideoEditAdapterVTest(MakeActivityVTest.this, wantData.get(0), 0);
        //linearLayoutManager.setStackFromEnd(true);
        //这是重点
        //linearLayoutManager.setReverseLayout(true);
        idRvData.setLayoutManager(linearLayoutManager);
        idRvData.setAdapter(videoEditAdapter);
        // idRvData.scrollBy(0,0);
        //linearLayoutManager.scrollToPositionWithOffset(0, 0);
        //layoutParams = (LinearLayout.LayoutParams) idRvData.getLayoutParams();
        setViewTreeObserver();
    }

    private void moveToPosition(final int n) {
//        int childCount = rvQuickEdit.getChildCount();
//        LogControl.i(TAG,"childCount==="+childCount+"n==="+n);
//        if(childCount-n<=3){
//            mJumpView.setVisibility(View.VISIBLE);
//        }

        //先从RecyclerView的LayoutManager中获取第一项和最后一项的Position
        int firstItem = linearLayoutManager.findFirstVisibleItemPosition();
        int lastItem = linearLayoutManager.findLastVisibleItemPosition();
        L_.e("moveToPosition  thisItme  :" + n);
        L_.e("moveToPosition  firstItem  :" + firstItem);
        L_.e("moveToPosition  lastItem  :" + lastItem);
        //然后区分情况
        if (n <= firstItem) {
            //当要置顶的项在当前显示的第一个项的前面时
            linearLayoutManager.scrollToPosition(n);
        } else if (n <= lastItem) {
            //当要置顶的项已经在屏幕上显示时
            int top = idRvData.getChildAt(n - firstItem).getTop();
            L_.e("moveToPosition:" + UIUtils.dp2px(80));
            L_.e("moveToPosition:" + top);
            idRvData.scrollBy(0, top);
        } else {
            //当要置顶的项在当前显示的最后一项的后面时
            idRvData.scrollToPosition(n);
            //这里这个变量是用在RecyclerView滚动监听里面的
            move = true;
        }

        idRvData.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
//                if (move) {
//                    move = false;
//                    int z = n - linearLayoutManager.findFirstVisibleItemPosition();
//                    if (z >= 0 && z < recyclerView.getChildCount()) {
//                        recyclerView.scrollBy(0, recyclerView.getChildAt(z).getTop()); //滚动到顶部
//                    }
//                }
            }
        });


    }

    private void setViewTreeObserver() {
        final ViewTreeObserver swipeRefreshLayoutVTO = idRvData.getViewTreeObserver();

        swipeRefreshLayoutVTO.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int[] locationMove = new int[2];
                idRvData.getLocationOnScreen(locationMove);//获取在整个屏幕内的绝对坐标
                int viewInWindowsX = locationMove[0];
                int viewInWindowsY = locationMove[1];
                L_.e("screenH＝viewInWindowsX " + viewInWindowsX + " &keyboardH viewInWindowsY= " + viewInWindowsY);


                Rect r = new Rect();
                idRvData.getWindowVisibleDisplayFrame(r);
                int statusBarH = getStatusBarHeight();//状态栏高度
                int screenH = idRvData.getRootView().getHeight();
                if (r.top != statusBarH) {
                    //在这个demo中r.top代表的是状态栏高度，在沉浸式状态栏时r.top＝0，通过getStatusBarHeight获取状态栏高度
                    r.top = statusBarH;
                }

                int keyboardH = screenH - (r.bottom - r.top);
                L_.e("screenH＝ " + screenH + " &keyboardH = " + keyboardH + " &r.bottom=" + r.bottom + " &top=" + r.top + " &statusBarH=" + statusBarH);

                if (keyboardH == currentKeyboardH) {//有变化时才处理，否则会陷入死循环
                    return;
                }

                currentKeyboardH = keyboardH;
                screenHeight = screenH;//应用屏幕的高度

                // editTextBodyHeight = videoEditAdapter.getmEditTextViews().getHeight();
                L_.e("screenH＝ " + screenH + " &keyboardH = " + keyboardH + " &r.bottom=" + r.bottom + " &top=" + r.top + " &editTextBodyHeight=" + editTextBodyHeight);
                if (keyboardH < 150) {//说明是隐藏键盘的情况
                    //updateEditTextBodyVisible(View.GONE, null);
                    return;
                }
//                //偏移listview
                if (linearLayoutManager != null) {
                    linearLayoutManager.scrollToPositionWithOffset(videoEditAdapter.getMpostion(), 1119);
                }
            }
        });
    }

    /**
     * 获取状态栏高度
     *
     * @return
     */
    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }


    //
//    /**
//     * 测量偏移量
//     * @param commentConfig
//     * @return
//     */
    private int getListviewOffset() {
        //这里如果你的listview上面还有其它占高度的控件，则需要减去该控件高度，listview的headview除外。
        //int listviewOffset = mScreenHeight - mSelectCircleItemH - mCurrentKeyboardH - mEditTextBodyHeight;
        int listviewOffset = screenHeight - currentKeyboardH - editTextBodyHeight - getToolBarView().getHeight();
        return UIUtils.dip2px(40);
    }


}
